package com.ysy.tz.mfg.equipment.rfid;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import purejavacomm.CommPortIdentifier;
import purejavacomm.SerialPort;
import purejavacomm.SerialPortEvent;
import purejavacomm.SerialPortEventListener;

public class Rfid_SAGE implements SerialPortEventListener {
    private static final Logger log = LoggerFactory.getLogger(Rfid_SAGE.class);
    InputStream inputStream;
    OutputStream outputStream;
    String portName;
    String baudrate;
    RfidCallback callback;
    String rfidPrefix;
    int rfidLength;
    private String lastRfid;

    public Rfid_SAGE(String portName, String baudrate) {
        this(portName, baudrate, (RfidCallback)null);
    }

    public Rfid_SAGE(String portName, String baudrate, RfidCallback callback) {
        this.baudrate = "9600";
        this.rfidPrefix = "aa5390807ff120";
        this.rfidLength = 5;
        this.lastRfid = null;
        this.portName = portName;
        if (baudrate != null && !baudrate.equals("")) {
            this.baudrate = baudrate;
        }

        this.callback = callback;
    }

    public void resetCallback(RfidCallback callback) {
        this.callback = callback;
    }

    public void init() throws Exception {
        Enumeration portList = CommPortIdentifier.getPortIdentifiers();

        while(portList.hasMoreElements()) {
            CommPortIdentifier portId = (CommPortIdentifier)portList.nextElement();
            if (portId.getPortType() == 1 && portId.getName().equals(this.portName)) {
                this.simpleRun(portId);
                break;
            }
        }

    }

    public void simpleRun(CommPortIdentifier portId) throws Exception {
        SerialPort serialPort = (SerialPort)portId.open("Rfid_SAGEReadApp", 1000);
        this.inputStream = serialPort.getInputStream();
        this.outputStream = serialPort.getOutputStream();
        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);
        serialPort.setSerialPortParams(Integer.parseInt(this.baudrate), 8, 1, 0);
        Thread.sleep(500L);
        this.start();
        Thread.sleep(500L);
        this.read();
    }

    void restart() throws Exception {
        String[] ss = new String[]{"55", "AC", "90", "08", "00", "01", "61", "EC", "30"};

        for(int i = 0; i < ss.length; ++i) {
            int ii = Integer.parseInt(ss[i], 16);
            this.outputStream.write(ii);
        }

        this.outputStream.flush();
    }

    void start() throws Exception {
        String[] ss = new String[]{"55", "AC", "00", "00", "00", "01", "0C", "66", "66"};

        for(int i = 0; i < ss.length; ++i) {
            int ii = Integer.parseInt(ss[i], 16);
            this.outputStream.write(ii);
        }

        this.outputStream.flush();
        System.out.println("RFID---start()完毕！");
    }

    void read() throws Exception {
        String[] ss = new String[]{"55", "AC", "00", "00", "00", "01", "63", "66", "66"};

        for(int i = 0; i < ss.length; ++i) {
            int ii = Integer.parseInt(ss[i], 16);
            this.outputStream.write(ii);
        }

        this.outputStream.flush();
        System.out.println("RFID---read()完毕！");
    }

    public void serialEvent(SerialPortEvent event) {
        if (event.getEventType() == 1) {
            DataModel dm = new DataModel(this.inputStream);

            try {
                dm.read();
                int command = dm.getCommand();
                switch (command) {
                    case 99:
                    default:
                        break;
                    case 255:
                        String rfid = dm.getEPC();
                        if (!rfid.equals(this.lastRfid) && rfid.length() == 8) {
                            this.lastRfid = rfid;
                            System.out.println("Rfid----->" + rfid);

                            try {
                                if (null != this.callback) {
                                    this.callback.receiveRfid(rfid);
                                }
                            } catch (Exception var6) {
                                log.error("rfid业务处理错误", var6);
                                var6.printStackTrace();
                            }
                        }
                }
            } catch (Exception var7) {
                log.error("rfid解析错误", var7);
            }
        }

    }

    public static void main(String[] args) throws Exception {
        Rfid_SAGE reader = new Rfid_SAGE("COM4", "9600", new RfidCallback() {
            public void receiveRfid(String rfid) throws Exception {
                System.out.println("rfid: " + rfid);
            }

            public void receiveEmpty() throws Exception {
                System.out.println("empty");
            }
        });
        reader.init();
    }

    public static class DataModel {
        int[] data = null;
        String[] hexdata = null;
        InputStream is = null;

        public DataModel(InputStream is) {
            this.is = is;
            this.data = new int[128];
        }

        public void read() throws Exception {
            this.data[0] = this.is.read();
            this.data[1] = this.is.read();
            this.data[2] = this.is.read();
            this.data[3] = this.is.read();
            this.data[4] = this.is.read();
            this.data[5] = this.is.read();
            int len = this.getLength();

            for(int i = 0; i < len; ++i) {
                this.data[6 + i] = this.is.read();
            }

            this.data[6 + len] = this.is.read();
            this.data[7 + len] = this.is.read();
            this.hexdata = this.toHexArray();
        }

        int getLength() {
            String str = Integer.toHexString(this.data[4]) + Integer.toHexString(this.data[5]);
            int len = Integer.parseInt(str, 16);
            return len;
        }

        String getID() {
            String id = Integer.toHexString(this.data[2]) + Integer.toHexString(this.data[3]);
            return id;
        }

        int getCommand() {
            return this.data[6];
        }

        String getData() {
            int len = this.getLength();
            StringBuffer sb = new StringBuffer();

            for(int i = 1; i < len; ++i) {
                String s = Integer.toHexString(this.data[6 + i]);
                s = s.replaceFirst("ffffff", "");
                sb.append(s);
            }

            return sb.toString();
        }

        String getEPC() {
            int len = this.getLength();
            StringBuffer sb = new StringBuffer();

            for(int i = 3; i < len; ++i) {
                String s = Integer.toHexString(this.data[6 + i]);
                s = s.replaceFirst("ffffff", "");
                if (s.length() == 1) {
                    s = "0" + s;
                }

                sb.append(s);
            }

            return sb.toString();
        }

        String[] toHexArray() {
            String[] hex = new String[this.data.length];

            for(int i = 0; i < this.data.length; ++i) {
                hex[i] = Integer.toHexString(this.data[i]);
            }

            return hex;
        }

        public String toString() {
            String str = "id:" + this.getID() + " cmd:" + this.getCommand() + " data:" + this.getData();
            return str;
        }
    }
}
